package com.xy.mvc.common;

import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @ClassName: CommonResult
 * @Description: TODO
 * @author: xy
 * @date: 2022/2/19  下午5:09
 */
@Data
public class CommonResult<T> implements Serializable {

    public static Integer CODE_SUCCESS = 0;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;


    public static <T> CommonResult<T> error(CommonResult<T> result) {
        return error(result.getCode(), result.getMessage());
    }

    public static <T> CommonResult<T> error(Integer code, String message) {
        Assert.isTrue(!CODE_SUCCESS.equals(code), "code 必须是错误的");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = CODE_SUCCESS;
        result.message = "";
        result.data = data;
        return result;
    }
}
